package com.aaa.operations.controller;

import com.aaa.common.entity.ResultData;
import com.sun.deploy.net.HttpRequest;
import javafx.application.Application;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.cloud.client.loadbalancer.reactive.Request;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * @author ：陈路遥
 * @date ：Created in 2020/8/7 15:00
 * @description：
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("/pan")
public class PanController {
    @RequestMapping("/addPan")

    public ResultData addPan(@RequestParam("file") MultipartFile file,
                             HttpServletRequest request, HttpServletResponse response){

        System.out.println("Aaaa");
        System.out.println(file.getSize());
        System.out.println(file.getName());
        try {
            System.out.println(file.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //想将MultipartFile对象中封装的文件保留达到tomcat上——
        //File file=new File("D:/file/ss.txt")
        String filePath=request.getSession().getServletContext().getRealPath("/fileUpload");
                //request.getSession().getServletContext().getRealPath("/fileUpload");

        System.out.println(filePath);
        return null;
    }

    private static final Logger logger = LoggerFactory.getLogger(Application.class);
    @RequestMapping(value = "/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file,@RequestParam("name")String name,HttpServletResponse response) {


        //response.setHeader("Access-Control-Allow-Origin","http://127.0.0.1:8020");


        logger.info("name: "+name);
        if (file.isEmpty()) {
            return "文件为空";
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        logger.info("上传的文件名为:" + fileName);
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        logger.info("上传的后缀名为:" + suffixName);
        // 文件上传路径
        String filePath = "d:/roncoo/ttt/";
        // 解决中文问题,liunx 下中文路径,图片显示问题
        //fileName = UUID.randomUUID() + suffixName;
        File dest = new File(filePath + fileName);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
            return "上传成功";
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败";
    }
    @RequestMapping("/sout")
    public void sout(Integer pageSize,Integer pageNum){
        System.out.println("Aaaaaaaaaqa");
        System.out.println(pageSize);
    }
}
